import key from 'keymaster';

function delay(timeout){
	return new Promise((resolve,reject)=>{
		setTimeout(()=>{
			resolve(timeout + '返回了' )
		},timeout)
	})
}

export default {
	namespace:"count",
	state : {
		record : 0 ,
		current : 0 ,
	},
	subscriptions:{
		keyboardWatcher({dispatch}){
			key('up',()=>{
				dispatch({type:'add'})
			})
			key('⌘+down, ctrl+down',()=>{
				console.log('ctrl + down or ⌘+down')
			})
		}
	},
	reducers : {
		add(state){
			const newCurrent = state.current + 1;
			return {
				record : newCurrent > state.record ? newCurrent : state.record ,
				current : newCurrent,
			}
		},
		minus(state){
			return {
				...state,
				current : state.current - 1,
			}
		}
	},
	effects : {
		*add(action,{ call, put }){
			const res = yield call(delay,1000);
			yield put({type:'minus'})
		}
	}
}